package com.level.system.task;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.*;

import com.level.system.entity.emr.DATA_HZ;
import com.level.system.service.EmrParseService;
import com.level.system.service.impl.HISUploadServiceImpl;
import org.apache.logging.log4j.util.PropertiesUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
 * 测试定时任务
 * @author zcw
 *
 */

import com.level.system.service.QcRuleService;
@Component
public class EmrParseTask{
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    private static List<Map<String,Object>> LIST;

    private final Logger log = LoggerFactory.getLogger(EmrParseTask.class);

    @Autowired
    private EmrParseService emrParseService;
    //每隔2秒执行一次
//    @Scheduled(fixedRate = 10000)
//    public void testTasks() {
//        System.out.println("定时任务执行时间：" + dateFormat.format(new Date()));
//    }

    //每天3：15执行
    @Scheduled(cron = "0 0/2 * * * ?")
//    @Scheduled(cron = "0 30 21 ? * *")
    public void getJgxx() {
        System.out.println("定时任务执行时间：" + dateFormat.format(new Date()));
        LIST = emrParseService.getJgxx();
//        List<Map<String,Object>> list = new ArrayList<>();
//        HashMap map = new HashMap();
//        map.put("jgid","006");
//        list.add(map);
//        LIST = list;
        System.out.println(LIST);
    }

    @Scheduled(cron = "0 0/10 * * * ?")
//    @Scheduled(cron = "0 30 23 ? * *")
    public void parseMzbl() {
        List<DATA_HZ> list = new ArrayList<>();
        //解析门诊 门诊病历
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("mzzy", "1");
        paramsMap.put("emrType", "60");
        long stat = new Date().getTime();
        //获取机构信息
        Map<String, Object> jgxxMap = null;
        // 创建一个可重用固定线程数的线程池
        ThreadPoolExecutor pool = new ThreadPoolExecutor(5, 10, 60, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(5));
        for(int k=0;k<LIST.size();k++){
            jgxxMap = LIST.get(k);
            paramsMap.put("jgid", jgxxMap.get("JGID"));
            System.out.println("parseMzbl getJgxx jgxxMap = " + jgxxMap);
            // 获取源病历数据
            list = emrParseService.queryMzbl(paramsMap);
            System.out.println("parseMzbl 病历源数据 list = " + list.size());
            log.info("parseMzbl getJgxx jgxxMap = " + jgxxMap);
            log.info("parseMzbl 病历源数据 list = " + list.size());
            if (list.size() > 0) {
                if (list.size() <= 30) {
                    // 生成task处理
                    Thread taskThread = new HZ_MZBLThread(list, emrParseService);
                    pool.execute(taskThread);
                } else {
                    int times = (int) Math.ceil(list.size() / 30.0);
                    System.out.println("parseMzbl 数据超过1000条分 " + times + "次插入！");
                    for (int i = 0; i < times; i++) {
                        System.out.println("parseMzbl 第" + i + "次插入！");
                        List<DATA_HZ> chunkList = list.subList(i * 30, Math.min((i + 1) * 30, list.size()));
                        // 生成task分批处理
                        Thread taskThread = new HZ_MZBLThread(chunkList, emrParseService);
                        pool.execute(taskThread);
                    }
                }
            }
        }
        pool.shutdown();
        long end = new Date().getTime();
        System.out.println("parseMzbl 耗时：" + (end - stat) + "毫秒");
    }

    /**
     * 解析入院记录
     */
    @Scheduled(cron = "0 0/8 * * * ?")
//    @Scheduled(cron = "0 40 23 ? * *")
    public void parseRyjl() throws Exception {
        List<DATA_HZ> list = new ArrayList<>();
        // 住院、归档、入院记录
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("mzzy", "2");
        paramsMap.put("state", "4");
        paramsMap.put("emrType", "3");
        paramsMap.put("archive", "3");

        long stat = new Date().getTime();
        //获取机构信息
        Map<String, Object> jgxxMap = null;
        // 创建一个可重用固定线程数的线程池
        ThreadPoolExecutor pool = new ThreadPoolExecutor(5, 10, 60, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(5));
        for(int k=0;k<LIST.size();k++) {
            jgxxMap = LIST.get(k);
            paramsMap.put("jgid", jgxxMap.get("JGID"));
            System.out.println("parseRyjl getJgxx jgxxMap = " + jgxxMap);
            // 获取源病历数据
            list = emrParseService.queryRyjl(paramsMap);
            System.out.println("parseRyjl 病历源数据 list = " + list.size());
            log.info("parseRyjl getJgxx jgxxMap = " + jgxxMap);
            log.info("parseRyjl 病历源数据 list = " + list.size());
            if (list.size() > 0) {
                if (list.size() <= 30) {
                    // 生成task处理
                    Thread taskThread = new HZ_RYJLThread(list, emrParseService);
                    pool.execute(taskThread);
                } else {
                    int times = (int) Math.ceil(list.size() / 30.0);
                    System.out.println("parseRyjl 数据超过1000条分 " + times + "次插入！");
                    for (int i = 0; i < times; i++) {
                        System.out.println("parseRyjl 第" + i + "次插入！");
                        List<DATA_HZ> chunkList = list.subList(i * 30, Math.min((i + 1) * 30, list.size()));
                        // 生成task分批处理
                        Thread taskThread = new HZ_RYJLThread(chunkList, emrParseService);
                        pool.execute(taskThread);
                    }
                }
            }
        }
        pool.shutdown();
        long end = new Date().getTime();
        System.out.println("parseRyjl 耗时：" + (end - stat) + "毫秒");
    }


    /**
     * 解析出院小结
     */
    @Scheduled(cron = "0 0/7 * * * ?")
//    @Scheduled(cron = "0 50 23 ? * *")
    public void parseCyxj() throws Exception {
        List<DATA_HZ> list = new ArrayList<>();
        // 住院、出院、出院小结
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("mzzy", "2");
        paramsMap.put("state", "4");
        paramsMap.put("emrType", "9");
        paramsMap.put("archive", "3");
        long stat = new Date().getTime();
        //获取机构信息
        Map<String, Object> jgxxMap = null;
        // 创建一个可重用固定线程数的线程池
        ThreadPoolExecutor pool = new ThreadPoolExecutor(5, 10, 60, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(5));
        for(int k=0;k<LIST.size();k++) {
            jgxxMap = LIST.get(k);
            paramsMap.put("jgid", jgxxMap.get("JGID"));
            System.out.println("parseCyxj getJgxx jgxxMap = " + jgxxMap);
            // 获取源病历数据
            list = emrParseService.queryCyxj(paramsMap);
            System.out.println("病历源数据 list = " + list.size());
            log.info("parseCyxj getJgxx jgxxMap = " + jgxxMap);
            log.info("病历源数据 list = " + list.size());
            if (list.size() > 0) {
                if (list.size() <= 30) {
                    // 生成task处理
                    Thread taskThread = new HZ_CYXJThread(list, emrParseService);
                    pool.execute(taskThread);
                } else {
                    int times = (int) Math.ceil(list.size() / 30.0);
                    System.out.println("parseCyxj 数据超过1000条分 " + times + "次插入！");
                    for (int i = 0; i < times; i++) {
                        System.out.println("parseCyxj 第" + i + "次插入！");
                        List<DATA_HZ> chunkList = list.subList(i * 30, Math.min((i + 1) * 30, list.size()));
                        // 生成task分批处理
                        Thread taskThread = new HZ_CYXJThread(chunkList, emrParseService);
                        pool.execute(taskThread);
                    }
                }
            }
        }
        pool.shutdown();
        long end = new Date().getTime();
        System.out.println("parseCyxj：" + (end - stat) + "毫秒");
    }


    /**
     * 解析24小时入出院记录  入院记录 出院记录
     */
    @Scheduled(cron = "0 0/5 * * * ?")
    public void parse24hRcyjl() {
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("mzzy", "2");
        paramsMap.put("state", "4");
        paramsMap.put("emrType", "10");
        paramsMap.put("archive", "3");

        long stat = new Date().getTime();
        Map<String, Object> jgxxMap = null;
        //获取机构信息
//        List<Map<String, Object>> jgxxMap = emrParseService.getJgxx();
        ThreadPoolExecutor pool = new ThreadPoolExecutor(5, 10, 30, TimeUnit.SECONDS, new ArrayBlockingQueue(5));
        for(int k=0;k<LIST.size();k++) {
            jgxxMap = LIST.get(k);
            paramsMap.put("jgid", jgxxMap.get("JGID"));
            System.out.println("parse24hRcyjl getJgxx jgxxMap = " + jgxxMap);
            log.info("parse24hRcyjl getJgxx jgxxMap = " + jgxxMap);
            // 获取源病历数据
            List<DATA_HZ> list = emrParseService.query24hRcyjl(paramsMap);
            System.out.println("parse24hRcyjl 病历源数据 list = " + list.size());
            log.info("parse24hRcyjl 病历源数据 list = " + list.size());


            if (list.size() > 0) {

                if (list.size() <= 30) {
                    // 生成task处理
                    Thread taskThread = new HZ_24HRCYJLThread(list, emrParseService);
                    pool.execute(taskThread);
                } else {
                    int times = (int) Math.ceil(list.size() / 30);
                    System.out.println("parse24hRcyjl 数据超过30条分 " + times + "次插入！");
                    for (int i = 0; i < times; i++) {
                        System.out.println("parse24hRcyjl 第" + i + "次插入！");
                        List<DATA_HZ> chunkList = list.subList(i * 30, Math.min((i + 1) * 30, list.size()));
                        // 生成task分批处理
                        Thread taskThread = new HZ_24HRCYJLThread(chunkList, emrParseService);
                        pool.execute(taskThread);
                    }
                }
            }
        }
        pool.shutdown();
        long end = new Date().getTime();

        System.out.println("parse24hRcyjl 耗时：" + (end - stat) + "毫秒");
    }


    /**
     * 解析死亡记录
     */
    @Scheduled(cron = "0 0/6 * * * ?")
    public void parseSwjl() {
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("mzzy", "2");
        paramsMap.put("state", "4");
        paramsMap.put("emrType", "11");
        paramsMap.put("archive", "3");

        long stat = new Date().getTime();
        //获取机构信息
        List<Map<String, Object>> jgxxMap = emrParseService.getJgxx();
        paramsMap.put("jgid", jgxxMap.get(0).get("JGID"));
        System.out.println("parseSwjl getJgxx jgxxMap = " + jgxxMap);
        // 获取源病历数据
        List<DATA_HZ> list = emrParseService.querySwjl(paramsMap);
        System.out.println("parseSwjl 病历源数据 list = " + list.size());

        ThreadPoolExecutor pool = new ThreadPoolExecutor(5, 10, 30, TimeUnit.SECONDS, new ArrayBlockingQueue(5));

        if (list.size() > 0) {

            if (list.size() <= 30) {
                // 生成task处理
                Thread taskThread = new HZ_SWJLThread(list, emrParseService);
                pool.execute(taskThread);
            } else {
                int times = (int) Math.ceil(list.size() / 30);
                System.out.println("parseSwjl 数据超过30条分 " + times + "次插入！");
                for (int i = 0; i < times; i++) {
                    System.out.println("parseSwjl 第" + i + "次插入！");
                    List<DATA_HZ> chunkList = list.subList(i * 30, Math.min((i + 1) * 30, list.size()));
                    // 生成task分批处理
                    Thread taskThread = new HZ_SWJLThread(chunkList, emrParseService);
                    pool.execute(taskThread);
                }
            }
        }
        pool.shutdown();
        long end = new Date().getTime();

        System.out.println("parseSwjl 耗时：" + (end - stat) + "毫秒");
    }

    /**
     * 解析病程记录
     */
    @Scheduled(cron = "0 0/4 * * * ?")
    public void parseBcjl() {
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("mzzy", "2");
        paramsMap.put("state", "4");
        paramsMap.put("emrType", "4");
        paramsMap.put("archive", "3");

        long stat = new Date().getTime();
        //获取机构信息
        Map<String, Object> jgxxMap = null;

        ThreadPoolExecutor pool = new ThreadPoolExecutor(5, 10, 30, TimeUnit.SECONDS, new ArrayBlockingQueue(5));
        for(int k=0;k<LIST.size();k++) {
            jgxxMap = LIST.get(k);
            paramsMap.put("jgid", jgxxMap.get("JGID"));
            log.info("parseBCJL getJgxx jgxxMap = " + jgxxMap);
            // 获取源病历数据
            List<DATA_HZ> list = emrParseService.queryZybl(paramsMap);
            log.info("==========================病程记录获取："+list);
            if (list.size() > 0) {
                if (list.size() <= 30) {
                    // 生成task处理
                    Thread taskThread = new HZ_BCJLThread(list, emrParseService);
                    pool.execute(taskThread);
                } else {
                    int times = (int) Math.ceil(list.size() / 30);
                    log.info("parseBcjl 数据超过30条分 " + times + "次插入！");
                    for (int i = 0; i < times; i++) {
                        log.info("parseBcjl 第" + i + "次插入！");
                        List<DATA_HZ> chunkList = list.subList(i * 30, Math.min((i + 1) * 30, list.size()));
                        // 生成task分批处理
                        Thread taskThread = new HZ_BCJLThread(chunkList, emrParseService);
                        pool.execute(taskThread);
                    }
                }
            }
        }
        pool.shutdown();
        long end = new Date().getTime();

        log.info("parseBcjl 耗时：" + (end - stat) + "毫秒");
    }
}
